#include <linux/module.h>
#include <linux/init.h>
#include <asm/atomic.h>
#include <linux/kthread.h>
#include <linux/delay.h>

#define err(msg) printk(KERN_ALERT "%s\n", msg)
#define NR 1

static struct task_struct *task[NR];

static int kthread_func(void *data)
{
	printk(" in %s()\n", __FUNCTION__);

	allow_signal(SIGKILL);
        while (1) {

                if (kthread_should_stop()||signal_pending(current))
                        break;
		set_current_state(TASK_INTERRUPTIBLE);
		printk(" jiffies is %lu\n", jiffies);

                //printk(KERN_ALERT "kthread_func.\n");
                msleep(5000);
        }

        return 0;
}

static int maininit(void)
{
	int i;
	for (i=0;i<NR;i++){
        if ((task[i] = kthread_create(kthread_func, NULL, "tmp%d", 0)) == NULL) {
                err("kthread_create");
                goto err;
        }
	else{
	       	printk("kthread_%d=ok.\n",i);
        	wake_up_process(task[i]);
		}
	}


        return 0;
err:
        return -1;
}

static void mainexit(void)
{
	int i;
	for (i=0;i<NR;i++)
        kthread_stop(task[i]);
}

module_init(maininit);
module_exit(mainexit);

MODULE_LICENSE("GPL");
